package com.hp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.dao.GoodDao;
import com.hp.dao.ItemDao;
import com.hp.dao.OrderDao;
import com.hp.entity.Item;
import com.hp.entity.Order;
import com.hp.service.IOrderService;
import com.hp.vo.ItemVo;
import com.hp.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiuQiang
 * @Date: 2021/10/29 9:32
 */
@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ItemDao itemDao;
    @Autowired
    private GoodDao goodDao;

    @Override
    public List<Order> findOrderAll(Integer status) {
        return orderDao.findOrderAll(status);
    }

    @Override
    public int orderUpdate(Integer id, Integer status) {
        return orderDao.orderUpdate(id,status);
    }

    @Override
    public int orderDelete(Integer order_id) {
        return orderDao.orderDelete(order_id);
    }

    @Override
    public List<OrderVo> findIndexOrderAll(Integer userId) {
        //根据user_id查询orders表的全部订单信息
        List<Order> orderList = orderDao.findOrderAllByUserId(userId);
        List<OrderVo> orderVoList = new ArrayList<>();
        for(Order order : orderList){
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(order,orderVo);
            //根据order_id查询全部items表全部信息
            List<Item> itemList = itemDao.findItemByOrderId(order.getId());
            List<ItemVo> itemVoList = new ArrayList<>();
            for (Item item : itemList){
                ItemVo itemVo = new ItemVo();
                BeanUtils.copyProperties(item,itemVo);
                //根据items表的good_id查询全部goods表信息存进item的good
                itemVo.setGood(goodDao.findGoodById(item.getGoodId()));
                itemVoList.add(itemVo);
            }
            orderVo.setItemVo(itemVoList);
            orderVoList.add(orderVo);
        }
        return orderVoList;
    }

    @Override
    public int orderSave(Order order) {
        return orderDao.orderSave(order);
    }

    @Override
    public Order findOrdersAllById(Integer id) {
        return orderDao.findOrdersAllById(id);
    }

    @Override
    public int orderPay(Order order) {
        return orderDao.orderPay(order);
    }
}
